import { BackSide, BoxGeometry, BufferAttribute, BufferGeometry, FrontSide, Mesh, MeshBasicMaterial, Object3D, Vector2, Vector3 } from "three";
import { App, GeoUtils, Point, Polygon } from "../../Engine";
import Helper from "../../utils/Helper";
import Const from "../../application/constants/Const";
import Events from "../../application/constants/Events";
import GeometryUtil from "../../renderExtension/utils/GeometryUtil";
import WallGeometry from "../../renderExtension/geometriy/WallGeometry";

export default class StereoPolygon {
    private data;
    private renderSide = FrontSide;
    private app: App;
    private wallMesh: Mesh;
    private roofMesh: Polygon;
    private centerPoint;
    private cameChangeListener;

    public constructor(data: {pointArray: Array<Point>, height: number, textureImg?: string, textureRoofImg?: string, renderOrder?: number}){
        this.data = data;

        this.centerPoint = Helper.getCenterPoint(data.pointArray).centerPoint;

        this.createRoofMesh(data);
        this.createWallMesh(data);

        this.app = App.getInstance();
        if(this.app.background.fog){
            this.cameChangeListener = this.changeCameraPosition.bind(this);
            this.app.on(Events.CameraChange, this.cameChangeListener);
            this.changeCameraPosition();
        }
    }

    private createWallMesh(data){
        let wallPoint = new Point(data.pointArray[0].x, data.pointArray[0].y, data.pointArray[0].z, Const.EPSGType.EPSGWeb).toEPSGWeb();
        let wallGeo = new WallGeometry(data.pointArray, data.height, wallPoint);
        let material = new MeshBasicMaterial({side: this.renderSide, depthTest: true, fog: false});
        this.wallMesh = new Mesh(wallGeo, material);
        this.wallMesh.position.copy(wallPoint);
        // mesh.position.z += data.height; 
        this.wallMesh.renderOrder = data.renderOrder || 0;
        App.getInstance().add(this.wallMesh);

        Helper.createTexture(data.textureImg, function (isSuccess, texture) {
            if (isSuccess) {
                material.map = texture;
                material.needsUpdate = true;
            }
        }, true);
    }

    private createRoofMesh(data){
        let polygonData = {
            pointArray: data.pointArray,  //多边形的顶点数组
            textureImg: (data.textureImg) ? data.textureImg : data.textureRoofImg, //贴在多边形上的图片
            alphaValue: 1,  //透明值
            height: data.height,
            depthTest: true,
            transparent: false
        };
        this.roofMesh = new Polygon(polygonData);
        this.roofMesh.renderOrder = data.renderOrder || 0;
        this.roofMesh.material['fog'] = false;
        App.getInstance().add(this.roofMesh);

        this.renderSide = this.roofMesh.renderSide;
    }

    public dispose(){
        App.getInstance().remove(this.roofMesh);
        App.getInstance().remove(this.wallMesh);
        this.app.off(Events.CameraChange, this.cameChangeListener);
    }

    private changeCameraPosition() {
        if(!this.app.background.fog){
            return;
        }
       
        let cameraPosition: Vector3 = this.app.three.camera.position;
        let distance = cameraPosition.distanceTo(this.centerPoint) * 0.7;
        if(distance >= this.app.background.fog.near){
            this.wallMesh.material['fog'] = true;
            this.roofMesh.material['fog'] = true;
        }else{
            this.wallMesh.material['fog'] = false;
            this.roofMesh.material['fog'] = false;
        }

        this.wallMesh.material['needsUpdate'] = true;
    }
}